home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 8 / QRZ Ham Radio Callsign Database - Volume 8.iso / pc / files / t_sys5 / unixkit.tgz / unixkit.tar / unixkit / common / bsd_io.c < prev    next >
C/C++ Source or Header  |  1991-12-28  |  11KB  |  515 lines

  1. /* OS- and machine-dependent stuff for 4.[23] BSD UNIX */
  2.  
  3. /*
  4.     FILE: UNIX.io.c
  5.     
  6.     Routines:
  7.         asy_init()
  8.         asy_stop()
  9.         asy_speed()
  10.         asy_output()
  11.         get_asy()
  12.         doasystat()
  13.         asy_ioctl()
  14.         asy_send()
  15.         asy_tx()
  16.         stxrdy()
  17.         asy_autobaud()
  18.         get_rlsd_asy()
  19.         asytimer()
  20.         dir()
  21.     Written or converted by Mikel Matthews, N9DVG, and others
  22.     
  23. */
  24.  
  25. #include <stdio.h>
  26. #include <fcntl.h>
  27. #include <sys/types.h>
  28. #include <sys/ioctl.h>
  29. #include <sys/file.h>
  30. #include <sys/dir.h>
  31. #include <sys/time.h>
  32. #include "global.h"
  33. #include "mbuf.h"
  34. #include "internet.h"
  35. #include "iface.h"
  36. #include "proc.h"
  37. #include "asy.h"
  38. #include "cmdparse.h"
  39. #include "devparam.h"
  40.  
  41. struct asy Asy[ASY_MAX];
  42. struct sgttyb mysavetty;
  43. unsigned Nasy = 0;
  44.  
  45. #ifdef USE_OUR_OWN_FCSTAB
  46. #ifndef PPP
  47. /*
  48.  * This table was lifted from ppp.c and stuck here until such time
  49.  * as we can get ppp to compile under UNIX... KA9WSB
  50.  *
  51.  * FCS lookup table as generated by fcsgen.c
  52.  */
  53. int16 fcstab[256] = {
  54.     0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf,
  55.     0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7,
  56.     0x1081, 0x0108, 0x3393, 0x221a, 0x56a5, 0x472c, 0x75b7, 0x643e,
  57.     0x9cc9, 0x8d40, 0xbfdb, 0xae52, 0xdaed, 0xcb64, 0xf9ff, 0xe876,
  58.     0x2102, 0x308b, 0x0210, 0x1399, 0x6726, 0x76af, 0x4434, 0x55bd,
  59.     0xad4a, 0xbcc3, 0x8e58, 0x9fd1, 0xeb6e, 0xfae7, 0xc87c, 0xd9f5,
  60.     0x3183, 0x200a, 0x1291, 0x0318, 0x77a7, 0x662e, 0x54b5, 0x453c,
  61.     0xbdcb, 0xac42, 0x9ed9, 0x8f50, 0xfbef, 0xea66, 0xd8fd, 0xc974,
  62.     0x4204, 0x538d, 0x6116, 0x709f, 0x0420, 0x15a9, 0x2732, 0x36bb,
  63.     0xce4c, 0xdfc5, 0xed5e, 0xfcd7, 0x8868, 0x99e1, 0xab7a, 0xbaf3,
  64.     0x5285, 0x430c, 0x7197, 0x601e, 0x14a1, 0x0528, 0x37b3, 0x263a,
  65.     0xdecd, 0xcf44, 0xfddf, 0xec56, 0x98e9, 0x8960, 0xbbfb, 0xaa72,
  66.     0x6306, 0x728f, 0x4014, 0x519d, 0x2522, 0x34ab, 0x0630, 0x17b9,
  67.     0xef4e, 0xfec7, 0xcc5c, 0xddd5, 0xa96a, 0xb8e3, 0x8a78, 0x9bf1,
  68.     0x7387, 0x620e, 0x5095, 0x411c, 0x35a3, 0x242a, 0x16b1, 0x0738,
  69.     0xffcf, 0xee46, 0xdcdd, 0xcd54, 0xb9eb, 0xa862, 0x9af9, 0x8b70,
  70.     0x8408, 0x9581, 0xa71a, 0xb693, 0xc22c, 0xd3a5, 0xe13e, 0xf0b7,
  71.     0x0840, 0x19c9, 0x2b52, 0x3adb, 0x4e64, 0x5fed, 0x6d76, 0x7cff,
  72.     0x9489, 0x8500, 0xb79b, 0xa612, 0xd2ad, 0xc324, 0xf1bf, 0xe036,
  73.     0x18c1, 0x0948, 0x3bd3, 0x2a5a, 0x5ee5, 0x4f6c, 0x7df7, 0x6c7e,
  74.     0xa50a, 0xb483, 0x8618, 0x9791, 0xe32e, 0xf2a7, 0xc03c, 0xd1b5,
  75.     0x2942, 0x38cb, 0x0a50, 0x1bd9, 0x6f66, 0x7eef, 0x4c74, 0x5dfd,
  76.     0xb58b, 0xa402, 0x9699, 0x8710, 0xf3af, 0xe226, 0xd0bd, 0xc134,
  77.     0x39c3, 0x284a, 0x1ad1, 0x0b58, 0x7fe7, 0x6e6e, 0x5cf5, 0x4d7c,
  78.     0xc60c, 0xd785, 0xe51e, 0xf497, 0x8028, 0x91a1, 0xa33a, 0xb2b3,
  79.     0x4a44, 0x5bcd, 0x6956, 0x78df, 0x0c60, 0x1de9, 0x2f72, 0x3efb,
  80.     0xd68d, 0xc704, 0xf59f, 0xe416, 0x90a9, 0x8120, 0xb3bb, 0xa232,
  81.     0x5ac5, 0x4b4c, 0x79d7, 0x685e, 0x1ce1, 0x0d68, 0x3ff3, 0x2e7a,
  82.     0xe70e, 0xf687, 0xc41c, 0xd595, 0xa12a, 0xb0a3, 0x8238, 0x93b1,
  83.     0x6b46, 0x7acf, 0x4854, 0x59dd, 0x2d62, 0x3ceb, 0x0e70, 0x1ff9,
  84.     0xf78f, 0xe606, 0xd49d, 0xc514, 0xb1ab, 0xa022, 0x92b9, 0x8330,
  85.     0x7bc7, 0x6a4e, 0x58d5, 0x495c, 0x3de3, 0x2c6a, 0x1ef1, 0x0f78
  86. };
  87. #endif
  88. #endif
  89.  
  90. /* Initialize asynch port "dev" */
  91. int
  92. asy_init(dev,iface,arg1,arg2,bufsize,trigchar,cts,rlsd,speed)
  93. int dev;
  94. struct iface *iface;
  95. char *arg1,*arg2;
  96. int16 bufsize;
  97. int trigchar;
  98. char cts;
  99. char rlsd;
  100. int16 speed;
  101. {
  102.     register struct asy *ap;
  103.     struct sgttyb sgttyb;
  104.     struct fifo *fp;
  105.     
  106.     if (dev > Nasy)
  107.         return(-1);
  108.  
  109.     ap = &Asy[dev];
  110. #ifdef notdef
  111.     if(!strncmp(arg2,"/dev/ptyp",8)) {
  112.         slipstart(arg2);
  113.         sleep(2);
  114.         arg2[5] = 't';
  115.     }
  116. #endif
  117.         
  118.     ap->tty = strdup(arg2);
  119.     ap->cts_flow_control = cts;
  120.     ap->rlsd_line_control = rlsd;
  121.     ap->trigchar = trigchar;
  122.     /* Set up receiver FIFO */
  123.     fp = &ap->fifo;
  124.     fp->buf = mallocw(bufsize);
  125.     fp->bufsize = bufsize;
  126.     fp->wp = fp->rp = fp->buf;
  127.     fp->cnt = 0;
  128.  
  129.     tprintf("asy_init: tty name = %s\n", ap->tty);
  130.     if ((ap->IORser = open (ap->tty, O_RDWR|O_NDELAY, 0)) < 0)
  131.     {
  132.         perror ("Could not open device for reading");
  133.         return (-1);
  134.     }
  135.     ++Nasy;
  136.     ap->iface = iface;
  137.     if ((ap->IOWser = open (ap->tty, O_WRONLY, 0)) < 0)
  138.     {
  139.         perror ("Could not open device for writing");
  140.         ap->IOWser = ap->IORser;
  141. #ifdef notdef
  142.         return (-1);
  143. #endif
  144.     }
  145.  
  146.  /* 
  147.   * get the stty structure and save it 
  148.   */
  149.  
  150.     if (ioctl (ap->IORser, TIOCGETP, &mysavetty) < 0)
  151.     {
  152.         perror ("ioctl failed on device");
  153.         return (-1);
  154.     }
  155.  /* 
  156.   * copy over the structure 
  157.   */
  158.  
  159.     sgttyb = mysavetty;
  160.     sgttyb.sg_flags = RAW;
  161.     sgttyb.sg_ispeed = sgttyb.sg_ospeed = B9600;
  162.  
  163.  /* 
  164.   * set up the parity and enter RAW mode 
  165.   */
  166.     sgttyb.sg_flags = (RAW | ANYP | CRMOD);
  167.  /* 
  168.   * now set the modes and flags 
  169.   */
  170.     if (ioctl (ap->IORser, TIOCSETP, &sgttyb) < 0)
  171.     {
  172.         perror ("ioctl could not set parameters for IORser");
  173.         return (-1);
  174.     }
  175.     if (ioctl (ap->IOWser, TIOCSETP, &sgttyb) < 0)
  176.     {
  177.         perror ("ioctl could not set parameters for IOWser");
  178.         return (-1);
  179.     }
  180.  
  181. /*
  182.     ++Nasy;
  183.     ap->iface = iface;
  184. */
  185.  
  186.     /* Enable SIGIO interrupt */
  187.     fcntl(ap->IORser,F_SETFL, fcntl(ap->IORser,F_GETFL,0) | FASYNC);
  188.     fcntl(ap->IORser,F_SETOWN,getpid());
  189.  
  190.     asy_speed(dev, speed);
  191.  
  192.     return (0);
  193. }
  194.  
  195. slipstart(buf)
  196. char *buf;
  197. {
  198.     FILE *fp;
  199.     int i;
  200.     if(fork())
  201.     return;
  202.     for(i=0; i < Nasy; ++i) {
  203.     close(Asy[i].IORser);
  204.     close(Asy[i].IOWser);
  205.     }
  206.     close(0);
  207.     close(1);
  208.     close(2);
  209.     fp = fopen(buf,"w+");
  210.     dup2(fileno(fp),0);
  211.     dup2(fileno(fp),1);
  212.     dup2(fileno(fp),2);
  213.     execl("/usr/local/etc/sliplogin","sliplogin",NULLCHAR);
  214. }
  215.  
  216. int
  217. asy_stop(iface)
  218. struct iface *iface;
  219. {
  220. }
  221.  
  222. /* Set asynch line speed */
  223. int
  224. asy_speed(dev,speed)
  225. int dev;
  226. int16 speed;
  227. {
  228.     struct sgttyb sgttyb;
  229.  
  230.     if(speed == 0 || dev >= Nasy)
  231.         return(-1);
  232.  
  233.     if (ioctl (Asy[dev].IORser, TIOCGETP, &sgttyb) < 0)
  234.     {
  235.         perror ("ioctl could not get parameters");
  236.         return (-1);
  237.     }
  238.     switch(speed)
  239.     {
  240.         case 0:
  241.             sgttyb.sg_ispeed = sgttyb.sg_ospeed = B0;
  242.             break;
  243.         case 50:
  244.             sgttyb.sg_ispeed = sgttyb.sg_ospeed = B50;
  245.             break;
  246.         case 75:
  247.             sgttyb.sg_ispeed = sgttyb.sg_ospeed = B75;
  248.             break;
  249.         case 110:
  250.             sgttyb.sg_ispeed = sgttyb.sg_ospeed = B110;
  251.             break;
  252.         case 134:
  253.             sgttyb.sg_ispeed = sgttyb.sg_ospeed = B134;
  254.             break;
  255.         case 150:
  256.             sgttyb.sg_ispeed = sgttyb.sg_ospeed = B150;
  257.             break;
  258.         case 200:
  259.             sgttyb.sg_ispeed = sgttyb.sg_ospeed = B200;
  260.             break;
  261.         case 300:
  262.             sgttyb.sg_ispeed = sgttyb.sg_ospeed = B300;
  263.             break;
  264.         case 600:
  265.             sgttyb.sg_ispeed = sgttyb.sg_ospeed = B600;
  266.             break;
  267.         case 1200:
  268.             sgttyb.sg_ispeed = sgttyb.sg_ospeed = B1200;
  269.             break;
  270.         case 1800:
  271.             sgttyb.sg_ispeed = sgttyb.sg_ospeed = B1800;
  272.             break;
  273.         case 2400:
  274.             sgttyb.sg_ispeed = sgttyb.sg_ospeed = B2400;
  275.             break;
  276.         case 4800:
  277.             sgttyb.sg_ispeed = sgttyb.sg_ospeed = B4800;
  278.             break;
  279.         case 9600:
  280.             sgttyb.sg_ispeed = sgttyb.sg_ospeed = B9600;
  281.             break;
  282.         case 19200:
  283.             sgttyb.sg_ispeed = sgttyb.sg_ospeed = EXTA;
  284.             break;
  285.         case 38400:
  286.             sgttyb.sg_ispeed = sgttyb.sg_ospeed = EXTB;
  287.             break;
  288.         /*
  289.         case EXTA:
  290.             sgttyb.sg_ispeed = sgttyb.sg_ospeed = EXTA;
  291.             break;
  292.         case EXTB:
  293.             sgttyb.sg_ispeed = sgttyb.sg_ospeed = EXTB;
  294.             break;
  295.         */
  296.         default:
  297.             tprintf("asy_speed: Unknown speed (%d)\n", speed);
  298.             break;
  299.     }
  300.  
  301.     if (ioctl (Asy[dev].IORser, TIOCSETP, &sgttyb) < 0)
  302.     {
  303.         perror ("ioctl could not set parameters for IORser");
  304.         return (-1);
  305.     }
  306.     if (ioctl (Asy[dev].IOWser, TIOCSETP, &sgttyb) < 0)
  307.     {
  308.         perror ("ioctl could not set parameters for IOWser");
  309.         return (-1);
  310.     }
  311.  
  312.     Asy[dev].speed = speed;
  313.     return(0);
  314. }
  315. /* Send a buffer to serial transmitter */
  316. void
  317. asy_output(dev,buf,cnt)
  318. int dev;
  319. char *buf;
  320. unsigned short cnt;
  321. {
  322.     int i_state;
  323.  
  324.     if(dev >= Nasy)
  325.         return;
  326.     i_state = dirps();
  327.     if ( write(Asy[dev].IOWser, buf, (int)cnt) < cnt)
  328.     {
  329.         perror("asy_output");
  330.         printf("asy_output: error in writing to device %d\n", dev);
  331.         restore (i_state);
  332.         return;
  333.     }
  334.     restore (i_state);
  335.     Asy[dev].txchar += cnt;
  336. }
  337.  
  338. /* Blocking read from asynch line
  339.  * Returns the character just read
  340.  */
  341. int
  342. get_asy(dev)
  343. int dev;
  344. {
  345.     char i_state;
  346.     register struct fifo *fp;
  347.     char c;
  348.  
  349.     fp = &Asy[dev].fifo;
  350.  
  351.     i_state = dirps();
  352.     while(fp->cnt == 0){
  353.         if(pwait(fp) != 0){
  354.             restore(i_state);
  355.             return -1;
  356.         }
  357.     }
  358.     fp->cnt--;
  359.     restore(i_state);
  360.  
  361.     c = *fp->rp++;
  362.     if(fp->rp >= &fp->buf[fp->bufsize])
  363.         fp->rp = fp->buf;
  364.  
  365.     return uchar(c);
  366. }
  367.  
  368. int32
  369. asy_ioctl(iface,cmd,set,val)
  370. struct iface *iface;
  371. int cmd;
  372. int set;
  373. int32 val;
  374. {
  375.     switch(cmd){
  376.     case PARAM_SPEED:
  377.         if(set)
  378.             asy_speed(iface->dev,(int16)val);
  379.         return Asy[iface->dev].speed;
  380. /*  This stuff should be added...  KA9WSB
  381.     case PARAM_DTR:
  382.         if(set)
  383.             writebit(base+MCR,MCR_DTR,(int)val);
  384.         return (inportb(base+MCR) & MCR_DTR) ? TRUE : FALSE;
  385.     case PARAM_RTS:
  386.         if(set)
  387.             writebit(base+MCR,MCR_RTS,(int)val);
  388.         return (inportb(base+MCR) & MCR_RTS) ? TRUE : FALSE;
  389.         case PARAM_DOWN:
  390.             clrbit(base+IER,(char)IER_DAV);
  391.             clrbit(base+MCR,MCR_RTS);
  392.             clrbit(base+MCR,MCR_DTR);
  393.             return FALSE;
  394.         case PARAM_UP:
  395.             setbit(base+IER,(char)IER_DAV);
  396.             setbit(base+MCR,MCR_RTS);
  397.             setbit(base+MCR,MCR_DTR);
  398.             return TRUE;
  399. */
  400.     };
  401.     return -1;
  402. }
  403.  
  404. int
  405. doasystat(argc,argv,p)
  406. int argc;
  407. char *argv[];
  408. void *p;
  409. {
  410.     register struct asy *asyp;
  411.  
  412.     for(asyp = Asy;asyp < &Asy[Nasy];asyp++)
  413.         if (asyp->tty != NULLCHAR)
  414.         tprintf("%s (%s %d baud): rxchar %lu txchar %lu\n",
  415.          asyp->iface->name,asyp->tty,asyp->speed,asyp->rxchar,asyp->txchar);
  416.     return 0;
  417. }
  418.  
  419. /* Send a message on the specified serial line */
  420. int
  421. asy_send(dev,bp)
  422. int dev;
  423. struct mbuf *bp;
  424. {
  425.     if(dev < 0 || dev >= Nasy)
  426.         return -1;
  427.     /* The data is sent directly rather than queued for asy_tx() */
  428.     while(bp != NULLBUF) {
  429.         asy_output(dev,bp->data,bp->cnt);
  430.            /* Now do next buffer on chain */
  431.         bp = free_mbuf(bp);
  432.     }
  433.     return 0;
  434. }
  435.  
  436. /* Serial transmit process, common to all protocols */
  437. void
  438. asy_tx(dev,p1,p2)
  439. int dev;
  440. void *p1;
  441. void *p2;
  442. {
  443. /* This function is not used. Instead, each process handles the output
  444.  * by itself. This is the way it used to be, and it works satisfactory
  445.  * on the Sun.
  446.  */
  447.     Asy[dev].iface->txproc = NULLPROC;
  448. }
  449.  
  450. int
  451. stxrdy(dev)
  452. int dev;
  453. {
  454.     return 1;
  455. }
  456.  
  457. void
  458. asytimer()
  459. {
  460. }
  461.  
  462. /* Wait for a signal that the RLSD modem status has changed */
  463. int
  464. get_rlsd_asy(dev, new_rlsd)
  465. int dev;
  466. int new_rlsd;
  467. {
  468.     int val;
  469.     if(ioctl(dev,TIOCMGET,&val) == -1)
  470.         return -1;
  471.     return val & TIOCM_CAR;
  472. }
  473.  
  474. /* Generate a directory listing by opening a pipe to /bin/ls.
  475.  * If full == 1, give a full listing; else return just a list of names.
  476.  */
  477. FILE *
  478. dir(path,full)
  479. char *path;
  480. int full;
  481. {
  482.     FILE *fp;
  483.     char cmd[1024];
  484.  
  485.     if (path == NULLCHAR || path[0] == '\0')
  486.         path = ".";
  487.  
  488.     if (full)
  489.         sprintf(cmd,"/bin/ls -lLF %s", path);
  490.     else
  491.         sprintf(cmd, "/bin/ls -CLF %s", path);
  492.  
  493.     if ((fp = popen(cmd,"r")) == NULLFILE) {
  494.         perror("popen");
  495.         return NULLFILE;
  496.     }
  497.  
  498.     return fp;
  499. }
  500. int
  501. getdir(path,full,file)
  502. char *path;
  503. int full;
  504. FILE *file;
  505. {
  506.     char buf[256];
  507.     FILE *fp;
  508.     if((fp = dir(path,full)) == NULLFILE)
  509.         return -1;
  510.     while(fread(buf,256,1,fp))
  511.         fwrite(buf,256,1,file);
  512.     fclose(fp);
  513.     return 0;
  514. }
  515.